package cn.sfrog.system.service.impl;

import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.system.domain.SysNoticeUserReadRecord;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.SysNoticeUserReadRecordFormVO;
import cn.sfrog.system.domain.vo.SysNoticeUserReadRecordQueryVO;
import cn.sfrog.system.domain.vo.SysNoticeUserReadRecordVO;
import cn.sfrog.system.mapper.SysNoticeUserReadRecordMapper;
import cn.sfrog.system.service.ISysNoticeUserReadRecordService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 通知公告用户已读关系Service业务层处理
 */
@Service
public class SysNoticeUserReadRecordServiceImpl extends ServiceImpl<SysNoticeUserReadRecordMapper, SysNoticeUserReadRecord> implements ISysNoticeUserReadRecordService {
    @Resource
    private SysNoticeUserReadRecordMapper sysNoticeUserReadRecordMapper;

    /**
     * 查询通知公告用户已读关系
     *
     * @param id 通知公告用户已读关系主键
     * @return 通知公告用户已读关系
     */
    @Override
    public SysNoticeUserReadRecordVO selectSysNoticeUserReadRecordById(Long id) {
        return sysNoticeUserReadRecordMapper.selectSysNoticeUserReadRecordById(id);
    }

    /**
     * 查询通知公告用户已读关系列表
     *
     * @param query 通知公告用户已读关系
     * @return 通知公告用户已读关系
     */
    @Override
    public List<SysNoticeUserReadRecordVO> selectSysNoticeUserReadRecordList(SysNoticeUserReadRecordQueryVO query) {
        return sysNoticeUserReadRecordMapper.selectSysNoticeUserReadRecordList(query);
    }

    /**
     * 新增通知公告用户已读关系
     *
     * @param form 通知公告用户已读关系
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertSysNoticeUserReadRecord(SysNoticeUserReadRecordFormVO form) {
        SysNoticeUserReadRecord sysNoticeUserReadRecord = new SysNoticeUserReadRecord();
        BeanUtils.copyProperties(form, sysNoticeUserReadRecord);
        sysNoticeUserReadRecord.setCreateTime(new Date());
        sysNoticeUserReadRecord.setCreateBy(SecurityUtils.getUsername());
        sysNoticeUserReadRecord.setUpdateTime(new Date());
        sysNoticeUserReadRecord.setUpdateBy(SecurityUtils.getUsername());
        return sysNoticeUserReadRecord.insert();
    }

    /**
     * 修改通知公告用户已读关系
     *
     * @param form 通知公告用户已读关系
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateSysNoticeUserReadRecord(SysNoticeUserReadRecordFormVO form) {
        SysNoticeUserReadRecord sysNoticeUserReadRecord = new SysNoticeUserReadRecord();
        BeanUtils.copyProperties(form, sysNoticeUserReadRecord);
        sysNoticeUserReadRecord.setUpdateTime(new Date());
        sysNoticeUserReadRecord.setUpdateBy(SecurityUtils.getUsername());
        return sysNoticeUserReadRecord.updateById();
    }

    /**
     * 批量删除通知公告用户已读关系
     *
     * @param ids 需要删除的通知公告用户已读关系主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteSysNoticeUserReadRecordByIds(Long[] ids) {
            return SysNoticeUserReadRecord.DAO.delete(Wrappers.lambdaQuery(SysNoticeUserReadRecord.class).in(SysNoticeUserReadRecord::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        //return SysNoticeUserReadRecord.DAO.update(Wrappers.lambdaUpdate(SysNoticeUserReadRecord.class).in(SysNoticeUserReadRecord::getId, (Object[]) vo.getIds()).set(SysNoticeUserReadRecord::getStatus, vo.getStatus()));
        return true;
    }
}
